package com.gxgy.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxgy.base.BaseAllMapper;
import com.gxgy.entity.Teacher;
import com.gxgy.service.TeacherService;
import com.gxgy.uilt.BaseUtil;
import com.gxgy.uilt.Id;
import com.gxgy.uilt.IfVersionException;
/**
 * @description: 教师表
 * @author: Qin Fengang
 * @date: 2021-11-19
 */
@Service
public class TeacherServiceImpl extends BaseAllMapper implements TeacherService {

	
	/**
	 * @description: 教师 查询 所有
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@Override
	public List<Teacher> queryAllTeacher() {
		
		return teacherMapper.selectList(null);
	}


	/**
	 * @description: 教师  添加 
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@Override
	public Integer addTeacher(String teacherNumber, String teacherName, String teacherSex, Integer teacherAge,
			String createTime,Model model) {
		if (teacherNumber.isEmpty()) {
			model.addAttribute("msg","教师工号不能为空");
			throw new IfVersionException("教师工号不能为空");
		}if (teacherName.isEmpty()) {
			model.addAttribute("msg","教师名字不能为空");
			throw new IfVersionException("教师名字不能为空");
		}if (teacherSex.isEmpty()) {
			model.addAttribute("msg","教师性别不能为空");
			throw new IfVersionException("教师性别不能为空");
		}if (teacherAge == null) {
			model.addAttribute("msg","教师年龄不能为空");
			throw new IfVersionException("教师年龄不能为空");
		}if (createTime.isEmpty()) {
			model.addAttribute("msg","创建时间不能为空");
			throw new IfVersionException("创建时间不能为空");
		}
		QueryWrapper<Teacher> wrapper = new QueryWrapper<Teacher>()
				.eq(Teacher.TEACHER_NAME, teacherName);
		Teacher queryByName = teacherMapper.selectOne(wrapper);
		if (queryByName != null) {
			throw new IfVersionException("该工号已存在");
		}
		String id = BaseUtil.getBaseId();
		Teacher teacher = new Teacher();
		teacher.setId(Id.thacher_+id);
		teacher.setTeacherNumber(teacherNumber);
		teacher.setTeacherName(teacherName);
		if (teacherSex.equals("男")) {
			teacher.setTeacherSex("0");
		}
		teacher.setTeacherSex("1");
		teacher.setTeacherAge(teacherAge);
		teacher.setCreateTime(createTime);
		
		return teacherMapper.insert(teacher);
	}

}
